package UserService

import (
	"context"
	"gdshop-font-api-go/app/request/UserRequest"
	"gdshop-font-api-go/library/response"
	"gdshop-font-api-go/library/tools/db"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

func Add(ctx context.Context, parames *UserRequest.Add) *response.JsonResponse {
	// 转换成类似数组的形式
	data := gconv.Map(parames)
	// 添加一下其他的数据
	data["create_at"] = gtime.Timestamp()
	// 插入数据
	result, err := db.GetUnSafaTable(ctx, "UserResponse").Insert(data)
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	// 获取到最后插入的自增ID
	newId, err2 := result.LastInsertId()
	if err2 != nil {
		return response.FailByRequestMessage(nil, err2.Error())
	}
	return response.SuccessByRequestMessageData(nil, "成功", g.Map{
		"id": newId,
	})
}
